package com.srs.backend.serviceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.srs.backend.mapper.ChefMapper;
import com.srs.backend.pojo.Chef;
import com.srs.backend.service.ChefService;
import com.srs.backend.utils.Result;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class ChefServicelmpl implements ChefService {
    @Resource
    private ChefMapper chefMapper;

    @Override
    public Result add(Map<String, String> data) {
        String name = data.get("name");
        String sex = data.get("sex");
        String phone = data.get("phone");
        String remark = data.get("remark");

        Integer uid;

        try {
            uid = Integer.parseInt(data.get("uid"));
        } catch (Exception e) {
            return Result.error("uid有误");
        }

        if (name == null || name.length() == 0) {
            return Result.error("厨师姓名不能为空");
        }

        if (phone.length() != 11) {
            return Result.error("号码长度错误");
        }

        QueryWrapper<Chef> chefQueryWrapper = new QueryWrapper<>();
        chefQueryWrapper.eq("name", name);
        List<Chef> chefs = chefMapper.selectList(chefQueryWrapper);
        if (!chefs.isEmpty()) {
            return Result.error("当前厨师已经存在");
        }
        chefMapper.insert(new Chef(null,name, sex, phone,remark, uid));
        return Result.ok("success");
    }

    @Override
    public Result delete(Map<String, String> data) {
        String idStr = data.get("id");
        Integer id = null;
        try {
            id = Integer.parseInt(idStr);
        } catch (NumberFormatException e) {
            return Result.error("id数据有误");
        }
        Chef curChef = chefMapper.selectById(id);
        if (curChef == null) {
            return Result.error("当前厨师不存在或已被删除");
        }
        chefMapper.deleteById(curChef);
        return Result.ok();
    }

    @Override
    public Result modify(Map<String, String> data) {
        String idStr = data.get("id");
        Integer id = null;
        try {
            id = Integer.parseInt(idStr);
        } catch (NumberFormatException e) {
            return Result.error("id数据有误");
        }
        Chef curChef = chefMapper.selectById(id);
        if (curChef == null) {
            return Result.error("当前厨师不存在或已被删除");
        }

        String name = data.get("name");
        if (name == null || name.length() == 0) {
            return Result.error("厨师姓名不能为空");
        }

        String sex = data.get("sex");
        String phone = data.get("phone");
        if (phone.length() != 11) {
            return Result.error("号码长度错误");
        }
        String remark = data.get("remark");


        curChef.setName(name);
        curChef.setSex(sex);
        curChef.setPhone(phone);
        curChef.setRemark(remark);

        chefMapper.updateById(curChef);

        return Result.ok();
    }

    @Override
    public Result getAll() {
        List<Chef> chefList = chefMapper.selectList(null);
        return Result.ok(chefList);

    }

    @Override
    public Result getChefInfo(Map<String, String> data) {
        String uidStr = data.get("uid");
        Integer uid;
        try {
            uid = Integer.parseInt(uidStr);
        } catch (Exception e) {
            return  Result.error("uid类型有误");
        }
        QueryWrapper<Chef> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uid", uid);
        Chef chef = chefMapper.selectOne(queryWrapper);
        if (chef == null) {
            return Result.error("该 厨师 不存在或已被删除");
        }
        return Result.ok(chef);
    }
}


